package o2sjsc.activity;

import java.util.ArrayList;
import java.util.HashMap;

import o2sjsc.core.libs.json.JSONParser;
import o2sjsc.core.libs.soap.SoapUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NewsAsyncTask extends
		AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {

	/** Declare variable */
	private Activity activity;
	private HashMap<String, String> paramInput;
	private String URL;
	private String METHOD_NAME;
	LazyAdapter adapter;
	ListView listView;
	TextView lblUploadData;
	ProgressDialog progressDialog;
	private ArrayList<HashMap<String, String>> output;
	static final String CONTENT_ID = "CONTENT_ID";
	static final String THUMB_IMG = "THUMB_IMG";
	static final String FULL_TITLE = "FULL_TITLE";
	static final String FULL_DESCRIPTION = "FULL_DESCRIPTION";

	/**
	 * @param activity
	 * @param progressBar
	 * @param paramInput
	 * @param uRL
	 * @param mETHOD_NAME
	 */
	public NewsAsyncTask(Activity activity, ListView listView,
			HashMap<String, String> paramInput, String uRL, String mETHOD_NAME,
			ArrayList<HashMap<String, String>> listOutput, TextView lblUpdateData) {
		super();
		this.activity = activity;
		this.paramInput = paramInput;
		this.listView = listView;
		URL = uRL;
		METHOD_NAME = mETHOD_NAME;
		output = new ArrayList<HashMap<String, String>>();
		output = listOutput;
		this.lblUploadData = lblUpdateData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPreExecute()
	 */
	@Override
	protected void onPreExecute() {
		progressDialog = new ProgressDialog(activity);
		// progressDialog.setTitle(ValueConstants.TITLE_LOADING);
		progressDialog.setMessage(ValueConstants.LOADING);
		progressDialog.show();
	}

	@Override
	protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
		try {
			String jsonString = SoapUtils.Invoke(URL, METHOD_NAME, paramInput);

			JSONParser jsonParser = new JSONParser();

			JSONArray jsonArray = jsonParser.getJSONFromString(jsonString);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject c = jsonArray.getJSONObject(i);
				String contentId = c.getString(CONTENT_ID);
				String thumbIMG = "http://cdn1.webbiresources.com/portals/"
						+ c.getString(THUMB_IMG);
				String title = c.getString(FULL_TITLE);
				String description = c.getString(FULL_DESCRIPTION);

				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(CONTENT_ID, contentId);
				map.put(THUMB_IMG, thumbIMG);
				map.put(FULL_TITLE, title);
				map.put(FULL_DESCRIPTION, description);
				output.add(map);
			}

			return output;
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
		adapter = null;
		if (result.size() > 0) {
			adapter = new LazyAdapter(activity, result);
			activity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// get listview current position - used to maintain scroll
					// position
					int currentPosition = listView.getFirstVisiblePosition();
					listView.setAdapter(adapter);
					// Setting new scroll position
					if (currentPosition != 0) {
						listView.setSelectionFromTop(currentPosition + 3, 0);
					}
					((BaseAdapter) adapter).notifyDataSetChanged();
					listView.refreshDrawableState();
					listView.invalidateViews();
					lblUploadData.setVisibility(View.INVISIBLE);
				}
			});
		}else{
			lblUploadData.setVisibility(View.VISIBLE);
			lblUploadData.setText(ValueConstants.UPDATE);
			listView.setVisibility(View.INVISIBLE);
		}
		// listView.setAdapter(adapter);
		progressDialog.dismiss();
	}
}
